//
//  rootTabBarViewController.m
//  FastRunDemo
//
//  Created by 苟亚明 on 16/9/16.
//  Copyright © 2016年 苟亚明. All rights reserved.
//

#import "rootTabBarViewController.h"
#import "KPNaviViewController.h"
#import "squareViewController.h"
#import "qiaoDanViewController.h"
#import "meViewController.h"
#import "dynamicViewController.h"
#import "GYMTabBar.h"
@interface rootTabBarViewController ()

@end

@implementation rootTabBarViewController
+ (instancetype)sharedRootVC {
    static rootTabBarViewController* manager = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[rootTabBarViewController alloc] init];
    });
    
    return manager;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [UINavigationBar appearance];
    
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    // 文字前面的颜色
    attrs[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
    
    // 通过appearance统一设置所有UITabBarItem的文字属性
    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:attrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
   
    
    // 添加子控制器
    [self setUpChildVc:[[squareViewController alloc]init] title:@"广场" image:@"tabBar_square" selectedImage:@"tabBar_square_select"];
    [self setUpChildVc:[[qiaoDanViewController alloc]init] title:@"抢单" image:@"tabBar_qiangdan" selectedImage:@"tabBar_qiangdan_select"];
    [self setUpChildVc:[[dynamicViewController alloc]init] title:@"我的动态" image:@"tabBar_dynamic" selectedImage:@"tabBar_dynamic_select"];
    [self setUpChildVc:[[meViewController alloc]init] title:@"个人中心" image:@"tabBar_personCenter" selectedImage:@"tabBar_personCenter_select"];
    
    // 更换tabBar
    [self setValue:[[GYMTabBar alloc]init] forKeyPath:@"tabBar"];

}


- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
    
    [UIApplication sharedApplication].keyWindow.rootViewController = self;
}

- (void)setUpChildVc:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage{
    
    vc.navigationItem.title = title;
    vc.tabBarItem.title = title;
    vc.tabBarItem.image = [UIImage imageNamed:image];
    vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
    
    KPNaviViewController *nav = [[KPNaviViewController alloc] initWithRootViewController:vc];
    [self addChildViewController:nav];
}

-(void)dealloc{
    GYMLog(@"dealloc -- rootTabBarVC");
}

@end
